package miniproj.models.groups;

import java.util.LinkedList;

import javax.xml.bind.annotation.*;

import miniproj.models.people.*;

public class Promotion implements java.io.Serializable {

	private static final long serialVersionUID = 1195758171070565289L;

	@XmlAttribute
	private String name;

	@XmlElement
	private Teacher headTeacher;

	@XmlElement
	private LinkedList<Group> groups;

	/**
	 * Instantiates a new promotion. (JavaBeans)
	 */
	public Promotion() {
		this.headTeacher = null;
		this.groups = new LinkedList<Group>();
		this.name = "undefined";
	}

	/**
	 * Instantiates a new promotion.
	 * 
	 * @param headTeacher
	 *            the head teacher of the promotion
	 * @param name
	 *            the name of the promotion
	 */
	public Promotion(Teacher headTeacher, String name) {
		this.headTeacher = headTeacher;
		this.groups = new LinkedList<Group>();
		this.name = name;
	}

	/**
	 * Instantiates a new promotion.
	 * 
	 * @param headTeacher
	 *            the head teacher of the promotion
	 * @param name
	 *            the name of the promotion
	 * @param groups
	 *            the groups of the promotion
	 */
	public Promotion(Teacher headTeacher, String name, LinkedList<Group> groups) {
		this.headTeacher = headTeacher;
		this.groups = groups;
		this.name = name;
	}

	/**
	 * Gets the groups of the promotion
	 * 
	 * @return the groups of the promotion
	 */
	public LinkedList<Group> getGroups() {
		return this.groups;
	}

	/**
	 * Gets the head teacher of the promotion
	 * 
	 * @return the head teacher of the promotion
	 */
	public Teacher getHeadTeacher() {
		return this.headTeacher;
	}

	/**
	 * Gets the promotion by head teacher name.
	 * 
	 * @param firstName
	 *            the first name of the teacher
	 * @param lastName
	 *            the last name of the teacher
	 * @param promotions
	 *            the promotions to seek into
	 * @return the promotion retrieved by head teacher name
	 */
	public static Promotion getPromotionByHeadTeacherName(String firstName,
			String lastName, LinkedList<Promotion> promotions) {
		Promotion promotion = null;
		for (Promotion p : promotions) {
			if (p.getHeadTeacher().equals(firstName, lastName)) {
				promotion = p;
				break;
			}
		}
		return promotion;
	}

	@Override
	public String toString() {
		return this.name;
	}

	/**
	 * Return the list of the teachings followed by this promotion.
	 * 
	 * @param allTeachings
	 *            the list of teachings
	 * @return the teachings followed by this promotion
	 */
	public LinkedList<Teaching> getFollowedTeachings(
			LinkedList<Teaching> allTeachings) {
		LinkedList<Teaching> toReturn = new LinkedList<Teaching>();

		boolean found = false;
		for (Teaching t : allTeachings) {
			for (Group p1 : t.getStudentsGroups()) {
				for (Group p2 : this.groups) {
					if (p1.equals(p2)) {
						toReturn.add(t);
						found = true;
						break;
					}
				}
				if (found)
					break;
			}
		}

		return toReturn;
	}
}